﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraGrid.Views.BandedGrid;

using ZhimeiSoft.Framework.Client.Base;
using ZhimeiSoft.CharmManager.DOL;
using ZhimeiSoft.Framework.Client.Utils;
using ZhimeiSoft.CharmManager.Client.Manager;
using ZhimeiSoft.CharmManager.Client.Interface;

namespace ZhimeiSoft.CharmManager.Client.Forms
{
    /// <summary>
    /// 代理单击表格事件
    /// </summary>
    /// <param name="drReservation">返回行</param>
    public delegate void EventOnClick(DataRow drReservation);
    /// <summary>
    /// 代理双击表格事件
    /// </summary>
    /// <param name="drReservation"></param>
    public delegate void EventOnDBClick(DataRow drReservation);

    public partial class TodayReservationList : BaseUserControl
    {
        /// <summary>
        /// 绑定的预约数据集
        /// </summary>
        private DataSet dsBindReservation;

        private DataTable dtEmployeeInfo = null;

        private DataTable dtCustomInfo = null;

        public EventOnClick OnGridClick;

        public EventOnDBClick OnGridDBClick;

        public DBEntityReservation ReservationInfo
        {
            get
            {
                if (advReservation.FocusedRowHandle < 0)
                    return null;

                DataRow drRow = advReservation.GetDataRow(advReservation.FocusedRowHandle);
                DBEntityReservation reservation = new DBEntityReservation(drRow);
                return reservation;
            }
        }

        public TodayReservationList()
        {
            InitializeComponent();
            /*if (this.advReservation.ViewStylesInfo != null)
            {
                //居中
                DevExpress.XtraGrid.Views.BandedGrid.BandedGridViewStylesInfo styleInfo = (BandedGridViewStylesInfo)this.advReservation.ViewStylesInfo;
                styleInfo.BandPanel.HAlignment = DevExpress.Utils.HorzAlignment.Center;
            }*/
            panNow.BackColor = DBEntityReservation.CUSTOMFUNC.GetColorResverationState("进行");
            panOver.BackColor = DBEntityReservation.CUSTOMFUNC.GetColorResverationState("完成");
            panPass.BackColor = DBEntityReservation.CUSTOMFUNC.GetColorResverationState("过期");
            panCancel.BackColor = DBEntityReservation.CUSTOMFUNC.GetColorResverationState("取消");


        }
        /// <summary>
        /// 绑定预约信息
        /// </summary>
        /// <param name="dsReservation"></param>
        public void BindData(DataSet dsReservation, DataTable dtEmployee, DataTable dtCustomInfo)
        {
            int hour = DateTime.Now.Hour;
            //fixColumn(hour);
            initBindDataSet(dsReservation, dtEmployee, dtCustomInfo);
        }
        /// <summary>
        /// 获取当前选择的行
        /// </summary>
        /// <returns></returns>
        public DataRow GetReservationDataRow()
        {
            if (advReservation.FocusedRowHandle < 0)
                return null;

            return advReservation.GetDataRow(advReservation.FocusedRowHandle);
        }
        /// <summary>
        /// 绑定预约数据
        /// </summary>
        /// <param name="dsReservation"></param>
        private void initBindDataSet(DataSet dsReservation, DataTable dtEmployee, DataTable dtCustom)
        {
            this.dtCustomInfo = dtCustom;
            this.dtEmployeeInfo = dtEmployee;

            dsBindReservation = dsReservation.Copy();
            DataTable dtReservation = dsBindReservation.Tables[0];
            dtReservation.Columns.Add(new DataColumn("NBR", typeof(int)));
            for (int i = 8; i <= 22; i++)
            {
                dtReservation.Columns.Add(new DataColumn(i + "00", typeof(string)));
                dtReservation.Columns.Add(new DataColumn(i + "15", typeof(string)));
                dtReservation.Columns.Add(new DataColumn(i + "30", typeof(string)));
                dtReservation.Columns.Add(new DataColumn(i + "45", typeof(string)));
            }
            int inbr = 1;
            foreach (DataRow drReservation in dtReservation.Rows)
            {
                drReservation["NBR"] = inbr;
                inbr++;
                //进行初始化数据
                //string fieldName = 
                DateTime startTime = Convert.ToDateTime(drReservation[DBEntityReservation.FieldName.RWORKSTARTTIME]);
                DateTime endTime = Convert.ToDateTime(drReservation[DBEntityReservation.FieldName.RWORKENDTIME]);
                if (startTime.Hour > 23)
                {
                    //系统不支持23点以后的预约记录
                    continue;
                }
                string fieldValue = DBEntityReservation.CUSTOMFUNC.GetResverationStateStr(drReservation[DBEntityReservation.FieldName.RSTATE].ToString());
                int iStartMinute = DBEntityReservation.CUSTOMFUNC.GetRoundMinute(startTime.Minute);
                int iEndMinute = DBEntityReservation.CUSTOMFUNC.GetRoundMinute(endTime.Minute);
                for (int i = startTime.Hour; i <= endTime.Hour; i++)
                {
                    string fieldName = string.Empty;
                    if (startTime.Hour == endTime.Hour)
                    {
                        //同一个小时内
                        BindHourMinuteData(i, iStartMinute, iEndMinute, drReservation, fieldValue);
                    }
                    else if (i == endTime.Hour)
                    {
                        //最后一个小时
                        BindHourMinuteData(i, 15, iEndMinute, drReservation, fieldValue);
                    }
                    else if (i == startTime.Hour)
                    {
                        //第一个小时
                        BindHourMinuteData(i, iStartMinute, 60, drReservation, fieldValue);
                    }else
                    {
                        BindHourMinuteData(i, 15, 60, drReservation, fieldValue);
                    }
                }                
            }
            advReservation.BeginUpdate();
            gdReservation.DataSource = dsBindReservation.Tables[0].DefaultView;
            advReservation.EndUpdate();
        }

        private void BindHourMinuteData(int hour, int startMinute, int endMinute, DataRow drRow, string fieldValue)
        {
            string fieldName = string.Empty;
            int iStartMinute = (startMinute == 0)?60:startMinute;
            int iEndMinute = (endMinute == 0)?60:endMinute;

            for (int i = iStartMinute; i <= iEndMinute; i = i + 15)
            {
                if (i == 60)
                {
                    fieldName = (hour+1).ToString() + "00";
                }
                else
                {
                    fieldName = hour.ToString() + i.ToString();
                }

                if (drRow.Table.Columns.IndexOf(fieldName) != -1)
                {
                    drRow[fieldName] = fieldValue;
                }
            }

        }

        private void SetEndMinuteBinData(int minute)
        {

        }

        private void gdReservation_Click(object sender, EventArgs e)
        {

        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            //int hour = DateTime.Now.Hour;
            //fixColumn(hour);
            
        }

        private void fixColumn(int hour)
        {
            if (hour < 8)
            {
                return;
            }
            if (hour > 8 )
            {
                for (int i = 2; i < advReservation.Bands.Count; i++)
                {
                    string captHour = advReservation.Bands[i].Caption;
                    int iCaptHour = -1;
                    if (Int32.TryParse(captHour, out iCaptHour))
                    {
                        if (iCaptHour == 19)
                        {
                            break;
                        }
                        if (iCaptHour <= hour)
                        {
                            advReservation.Bands[i].Fixed = DevExpress.XtraGrid.Columns.FixedStyle.Left;
                        }
                        else
                        {
                            break;
                        }
                    }
                }
            }
        }

        private void advReservation_CustomDrawBandHeader(object sender, BandHeaderCustomDrawEventArgs e)
        {
             
            Rectangle r = e.Bounds;
            int iHour = 0;
            Brush ellipseBrush = Brushes.LightCyan;
            if (e.Band.BandLevel == 0)
            {
                //画列头的颜色
                if (!GetBandBrush(e.Band.Caption, out ellipseBrush, out iHour))
                {
                    return;
                }
                
            }
            else if (e.Band.BandLevel == 1)
            {
                //画第二排头的颜色
                if (!GetBandBrush(e.Band.ParentBand.Caption, out ellipseBrush, out iHour))
                {
                    return;
                }
            }
            else
            {
                return;
            }
            //ellipseBrush = Brushes.LightSkyBlue;
            if (DateTime.Now.Hour == iHour)
            {
                ellipseBrush = Brushes.LightSkyBlue;
            }
            ControlPaint.DrawBorder3D(e.Graphics, r, Border3DStyle.Bump);
            r.Inflate(-1, -1);
            e.Graphics.FillRectangle(ellipseBrush, r);
            r.Inflate(-2, 0);
            e.Style.DrawString(e.Graphics, e.Band.Caption, r);
            e.Handled = true;
            
        }

        private bool GetBandBrush(string bandCaption, out Brush bandBrush, out int iHour)
        {
            bandBrush = Brushes.LightCyan;
            string sTime = bandCaption.Replace("点", "");
            if (!Int32.TryParse(sTime, out iHour))
            {
                return false;
            }
            int result = 0;
            Math.DivRem(iHour, 2, out result);
            if (result == 0)
            {
                bandBrush = Brushes.LightGoldenrodYellow;
            }
            return true;
        }

        private void advReservation_CustomDrawCell(object sender, DevExpress.XtraGrid.Views.Base.RowCellCustomDrawEventArgs e)
        {
            if (e.RowHandle < 0)
            {
                return;
            }
            if (e.CellValue == null || e.CellValue.ToString().Equals(string.Empty))
            {
                return;
            }
            int icolumnName = -1;
            if (Int32.TryParse(e.Column.Caption, out icolumnName))
            {
                //为数字列
                Rectangle r = e.Bounds;
                Brush ellipseBrush = Brushes.White;

                Color drawColor = DBEntityReservation.CUSTOMFUNC.GetColorResverationState(e.CellValue.ToString());
                ellipseBrush = new SolidBrush(drawColor);
                e.Graphics.FillRectangle(ellipseBrush, r);
                e.Style.DrawString(e.Graphics, e.DisplayText, r);
                e.Handled = true;
            }
        }

        private void advReservation_Click(object sender, EventArgs e)
        {
            if (advReservation.FocusedRowHandle < 0)
                return;

            DataRow drReservation = advReservation.GetDataRow(advReservation.FocusedRowHandle);
            if (OnGridClick != null)
            {
                OnGridClick(drReservation);
            }
        }

        private void advReservation_DoubleClick(object sender, EventArgs e)
        {
            if (advReservation.FocusedRowHandle < 0)
                return;

            DataRow drReservation = advReservation.GetDataRow(advReservation.FocusedRowHandle);
            if (OnGridDBClick != null)
            {
                OnGridDBClick(drReservation);
            }
        }

    }
}
